﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Xml;
using System.IO;
using System.Security.Cryptography;

public partial class kecheng_mp_sort : System.Web.UI.Page
{
    public string url = "";
    string xmlPath = "/lapUpLoad/temp/";
    public SqlCommand com;
    public int title_id = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        //初始化一个command对象
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["gConnectionString"]);
        conn.Open();
        com = new SqlCommand();
        com.Connection = conn;
        if (!IsPostBack)
        {
            Session["xmlFileName"] = DateTime.Now.ToFileTime().ToString();
            if (Request.QueryString["title_id"] != null)
            {
                title_id = Convert.ToInt32(Request.QueryString["title_id"]);
                toList(title_id);
                url = System.Configuration.ConfigurationManager.AppSettings["php"] + string.Format("thumbs.php?xpath=temp/{0}.xml", Session["xmlFileName"].ToString());
            }
            else
            {
                Response.Write("<script language='javascript'>alert('非法进入!');</script>");
            }

        }
    }

    protected bool toList(int title_id)
    {

        string sql = "select * from imgUpLoad where title_id=@titleId and convertStatus!=6";
        com.CommandType = CommandType.Text;
        com.CommandText = sql;
        com.Parameters.AddWithValue("@titleId", title_id);
        SqlDataReader dr = com.ExecuteReader();
        if (dr.HasRows)
        {
            toXml(dr);
            return true;
        }
        else
        {
            Response.Write("<script language='javascript'>alert('你还没有在此课程下上传视频！');window.location.href='upLoad.aspx?title_id=" + title_id + "';</script>");
            return false;
        }
    }
    protected bool toXml(SqlDataReader dr)
    {
        XmlDocument xmlDoc = new XmlDocument();
        XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
        xmlDoc.AppendChild(xmlDec);
        XmlNode playlist = xmlDoc.CreateElement("playList");
        xmlDoc.AppendChild(playlist);

        XmlNode trackList = xmlDoc.CreateElement("trackList");
        playlist.AppendChild(trackList);

        while (dr.Read())
        {
            XmlNode track = xmlDoc.CreateElement("track");
            trackList.AppendChild(track);
            XmlNode name = xmlDoc.CreateElement("name");
            name.InnerText = dr["f_fileName"].ToString();
            track.AppendChild(name);
            XmlElement thumb = xmlDoc.CreateElement("thumb");
            thumb.InnerText = Path.GetFileNameWithoutExtension(dr["f_fileName"].ToString())+".jpg";
            track.AppendChild(thumb);
            XmlElement flv = xmlDoc.CreateElement("flv");
            flv.InnerText = dr["f_fileName"].ToString();
            track.AppendChild(flv);
            XmlElement location = xmlDoc.CreateElement("location");
            location.InnerText = dr["f_fileName"].ToString();
            track.AppendChild(location);
            XmlElement size = xmlDoc.CreateElement("size");
            size.InnerText = "0";
            track.AppendChild(size);
            XmlElement trueName = xmlDoc.CreateElement("trueName");
            trueName.InnerText = dr["f_trueName"].ToString();
            track.AppendChild(trueName);
            XmlElement sort = xmlDoc.CreateElement("sortid");
            sort.InnerText = dr["innerOrder"].ToString();
            track.AppendChild(sort);
        }

        string playListFile = MapPath("../") + xmlPath + Session["xmlFileName"].ToString() + ".xml";
        xmlDoc.Save(playListFile);
        xmlDoc.RemoveAll();
        dr.Close();
        return true;
    }

    bool upDb()
    {
        return true;
    }

    protected void Button10_Click(object sender, EventArgs e)
    {
        string playListFile = MapPath("../") + xmlPath + Session["xmlFileName"].ToString() + ".xml";
        if (System.IO.File.Exists(playListFile))
        {
            if (readXmlFile())
            {
                Response.Write("<script language='javascript'>alert('排序保存成功！');window.location.href='upLoadimage/mulu_view.aspx?title_id=" + title_id + "';</script>");
            }
            else
            {
                Response.Write(string.Format("<script language='javascript'>alert('{0}');</script>", "读取失败！"));
            }
            //Response.Redirect(string.Format("upLoadimage/mulu_view.aspx?title_id={0}", title_id));
        }
        else
        {
            Response.Write("<script language='javascript'>alert('没有待排序内容！自动跳转到上传页面');window.location.href='upLoad.aspx?title_id=" + title_id + "';</script>");
        }

    }

    protected bool readXmlFile()
    {
        //遍历xml文件
        XmlDocument xmlDoc = new XmlDocument();
        string path = MapPath("../") + xmlPath + Session["xmlFileName"].ToString() + ".xml";
        if (System.IO.File.Exists(path))
        {
            xmlDoc.Load(path);
            XmlNodeList nodeList = xmlDoc.GetElementsByTagName("track");
            foreach (XmlNode item in nodeList)
            {
                string fileName = Path.GetFileNameWithoutExtension(item.SelectSingleNode("name").InnerText) + ".flv";
                string trueName = item.SelectSingleNode("trueName").InnerText;
                string sortId = item.SelectSingleNode("sortid").InnerText;
                updateDb(fileName, trueName, sortId);
            }
            return true;
        }
        else
        {
            Response.Write(string.Format("<script language='javascript'>alert('{0}');</script>", "没有找到文件!"));
            return false;
        }
    }
    bool updateDb(string fileName, string trueName, string sortid)
    {
        //将xml文件内的数据插入的数据库中
        string sql = "update imgUpLoad set innerOrder=@innerOrder where  f_fileName=@f_fileName";
        com.CommandType = CommandType.Text;
        com.CommandText = sql;
        com.Parameters.Clear();
        com.Parameters.AddWithValue("@title_id", title_id);
        com.Parameters.AddWithValue("@f_fileName", fileName);
        com.Parameters.AddWithValue("@innerOrder", sortid);
        int result = com.ExecuteNonQuery();
        return true;
    }

}
